import {
	chargeUrl,
	two_chargeUrl
} from '@/config'
// import config from '../api/chongdian/index'
import store from '@/store'
import {
	getToken
} from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import {
	toast,
	showConfirm,
	tansParams
} from '@/utils/common'

let timeout = 30000
let baseUrl = ''
let platform = uni.getSystemInfoSync().uniPlatform


const request = config => {
	// 是否需要设置 token
	const isToken = (config.headers || {}).isToken === false
	config.header = config.header || {}
	let token = getToken()
	// if (token && !isToken) {
	// 	config.header['Authorization'] = 'Bearer ' + token
	// }
	baseUrl = config.url.includes('mpapi') ? chargeUrl : two_chargeUrl



	// get请求映射params参数
	if (config.params) {
		let url = config.url + '?' + tansParams(config.params)
		url = url.slice(0, -1)
		config.url = url
	}

	// console.log('config', config);

	return new Promise((resolve, reject) => {


		uni.request({
				method: config.method || 'get',
				timeout: config.timeout || timeout,
				url: baseUrl + config.url,
				data: config.data,
				header: {
					...config.header,
					token: getToken()
				},
				dataType: 'json'
			}).then(response => {
				// console.log('response',response);
				let [error, res] = response
				if (error) {
					toast('网络状态不佳，请重试')
					reject('网络状态不佳，请重试')
					return
				}
				const code = res.data.code || 200
				const msg = errorCode[code] || res.data.msg || errorCode['default']
				if (code === 401) {

					reject('401')
				} else if (code === 500) {
					toast(msg)
					reject('500')
				} else if (code !== 200) {
					toast(msg)
					reject(code)
				}
				resolve(res.data)
			})
			.catch(error => {
				let {
					message
				} = error
				if (message === 'Network Error') {
					// message = '后端接口连接异常'
					message = '网络状态不佳，请重试'
				} else if (message.includes('timeout')) {
					// message = '系统接口请求超时'
					message = '网络状态不佳，请重试'
				} else if (message.includes('Request failed with status code')) {
					message = '系统接口' + message.substr(message.length - 3) + '异常'
				}
				toast(message)
				reject(error)
			})
	})
}
/**
 * 封封微信的的request
 */
function request1(config) {
	baseUrl = config.url.includes('mpapi') ? chargeUrl : two_chargeUrl

	config.header = config.header || {}

	// get请求映射params参数
	if (config.params) {
		let url = config.url + '?' + tansParams(config.params)
		url = url.slice(0, -1)
		config.url = url
	}

	// console.log('config', config);
	// let data = JSON.stringify(config.data)
	return new Promise(function(resolve, reject) {
		uni.request({
			url: baseUrl + config.url,
			data: config.data, //config.data
			method: config.method,
			timeout: config.timeout || timeout,
			header: {
				...config.header,
				token: getToken()
			},
			dataType: 'json',
			success: function(res) {
				const code = res.data.code
				const msg = errorCode[code] || res.data.msg || errorCode['default']
				if (code !== 200) {
					toast(msg)
					reject(code)
				}

				resolve(res.data);
			},
			complete: function(e) {
				// console.log('e', e);
				if (e?.data.code == 102) {

					store.dispatch('user/LogOut').then(res => {
						uni.reLaunch({
							url: '/pages/mine/index'
						})
					})
				}

			},
			fail: function(err) {
				console.log('err', err);
				// let {
				// 	message
				// } = error
				let message = err.errMsg
				if (message === 'Network Error') {
					// message = '后端接口连接异常'
					message = '网络状态不佳，请重试'
				} else if (message.includes('timeout')) {
					// message = '系统接口请求超时'
					message = '网络状态不佳，请重试'
				} else if (message.includes('request:fail')) {
					message = '系统接口异常'
				}
				toast(message)
				reject(err);
				console.log('微信网络请求异常', err);
			}
		});
	});
}

export default request1